package com.study.samsung.Dao;

import com.study.samsung.Entity.*;
import com.study.samsung.Utils.RowMapper;
import com.study.samsung.Utils.SQLHelper;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class ProductDetailsDao {
    SQLHelper helper = new SQLHelper();

    public List<Commodity> selectAll(Integer lid){
        String sql = "SELECT * FROM commodity a LEFT JOIN detail b ON a.sid=b.sid " +
                "LEFT JOIN commoditymemory c ON b.nid=c.nid " +
                "LEFT JOIN commoditycolor d ON b.yid = d.yid " +
                "WHERE lid=? GROUP BY b.sid";
        return helper.query(sql,new CommodityRowMapper(),lid);
    }
    public Commodity selectOne(Integer sid,Integer nid){
        String sql = "SELECT * FROM commodity a LEFT JOIN detail b ON a.sid=b.sid " +
                "LEFT JOIN commoditymemory c ON b.nid=c.nid " +
                "LEFT JOIN commoditycolor d ON b.yid = d.yid " +
                "WHERE b.sid=? and b.nid=?";
        return helper.one(sql,new CommodityRowMapper(),sid,nid);
    }

//    从商品列表页进入默认第一个商品
    public Commodity selectSid (Integer lid){
        String sql = "SELECT * FROM commodity a LEFT JOIN detail b ON a.sid=b.sid " +
                "LEFT JOIN commoditymemory c ON b.nid=c.nid " +
                "LEFT JOIN commoditycolor d ON b.yid = d.yid" +
                " WHERE lid=? GROUP BY lid";
        return helper.one(sql,new CommodityRowMapper(),lid);
    }

    public List<Commoditymemory> selectmemory(Integer sid){
        String sql = "SELECT d.*,c.nname FROM detail d LEFT JOIN commoditymemory c ON d.nid=c.nid WHERE d.sid=? GROUP BY nid,d.sid ORDER BY d.sxid";
        return helper.query(sql,new CommoditymemoryRowMapper(),sid);
    }
    public Commoditymemory selectmemoryone(Integer sid,Integer nid){
        String sql = "SELECT d.*,c.nname FROM detail d LEFT JOIN commoditymemory c ON d.nid=c.nid WHERE d.sid=? and d.nid=? GROUP BY nid,d.sid ORDER BY d.sxid";
        return helper.one(sql,new CommoditymemoryRowMapper(),sid,nid);
    }

    public List<Commoditycolor> selectcolor(){
        String sql = "select * from commoditycolor";
        return helper.query(sql,new CommoditycolorRowMapper());
    }

    public List<Commodityimg> selectimg(Integer sid){
        String sql = "SELECT * FROM commodity c LEFT JOIN commodityimg i ON c.sid=i.sid WHERE c.sid=? and classify=1";
        return helper.query(sql,new CommodityimgRowMapper(),sid);
    }
    public List<Commodityimg> selectxiaimg(Integer sid){
        String sql = "SELECT * FROM commodity c LEFT JOIN commodityimg i ON c.sid=i.sid WHERE c.sid=? and classify=0";
        return helper.query(sql,new CommodityimgRowMapper(),sid);
    }

    public Integer insertLike(Integer uid,Integer sid,String date){
        String sql = "INSERT INTO collect VALUES(NULL,?,?,?)";
        return helper.update(sql,date,sid,uid);
    }
    public Collect selectByOne(Integer sid,Integer uid){
        String sql = "SELECT * FROM collect WHERE sid =? AND uid = ?";
        return helper.one(sql, new RowMapper<Collect>() {
            @Override
            public Collect map(ResultSet rs) throws SQLException {
                Collect col = new Collect();
                col.setSid(rs.getInt("sid"));
                return col;
            }
        },sid,uid);
    }
    public Integer deleteOne(Integer sid){
        String sql = "DELETE FROM collect WHERE sid=?";
        return helper.update(sql,sid);
    }

    public Integer addCart(Integer num, Integer sxid, Integer uid, BigDecimal gprice){
        String sql = "INSERT INTO shopping_cart VALUES(NULL,?,?,?,?)";
        return helper.update(sql,num,sxid,uid,gprice);
    }
    public Integer updateCart(Integer sxid,Integer gnum,BigDecimal gprice){
        String sql = "UPDATE shopping_cart SET gnum=?,gprice=? WHERE sxid=?";
        return helper.update(sql,gnum,gprice,sxid);
    }
    public ShoppingCart addCartOne(Integer sxid){
        String sql = "SELECT * FROM shopping_cart WHERE sxid=?";
        return helper.one(sql, new RowMapper<ShoppingCart>() {
            @Override
            public ShoppingCart map(ResultSet rs) throws SQLException {
                ShoppingCart scart = new ShoppingCart();
                scart.setGid(rs.getInt("gid"));
                scart.setGnum(rs.getInt("gnum"));
                scart.setSxid(rs.getInt("sxid"));
                scart.setUid(rs.getInt("uid"));
                scart.setGprice(rs.getBigDecimal("gprice"));
                return scart;
            }
        },sxid);

    }

    public Detail selectOneDetail(Integer nid,Integer yid,Integer sid){
        String sql = "SELECT * FROM detail WHERE nid=? AND yid=? AND sid=?";
        return helper.one(sql,new DetailRowMapper(),nid,yid,sid);
    }

//    查询订单也要的参数
    public List<Commodity> buy(Integer sxid){
        String sql = "SELECT * FROM commodity a LEFT JOIN detail b ON a.sid=b.sid " +
                "LEFT JOIN commoditymemory c ON b.nid=c.nid " +
                "LEFT JOIN commoditycolor d ON b.yid = d.yid " +
                "WHERE sxid=?";
        return helper.query(sql,new CommodityRowMapper(),sxid);
    }

    public List<Detail> prohibit(Integer nid,Integer sid){
        String sql = "SELECT * FROM detail a LEFT JOIN commoditymemory b ON a.nid = b.nid LEFT JOIN commoditycolor c ON a.yid=c.yid WHERE a.nid=?  AND a.sid=?";
        return helper.query(sql, new RowMapper<Detail>() {
            @Override
            public Detail map(ResultSet rs) throws SQLException {
                Detail detail = new Detail();
                detail.setSxid(rs.getInt("sxid"));
                detail.setXprice(rs.getBigDecimal("xprice"));
                detail.setYid(rs.getInt("yid"));
                detail.setNid(rs.getInt("nid"));
                detail.setSid(rs.getInt("sid"));
                return detail;
            }
        },nid,sid);
    }
    public List<Detail> prohibitColor(Integer yid,Integer sid){
        String sql = "SELECT * FROM detail a LEFT JOIN commoditymemory b ON a.nid = b.nid LEFT JOIN commoditycolor c ON a.yid=c.yid WHERE a.yid=?  AND a.sid=?";
        return helper.query(sql, new RowMapper<Detail>() {
            @Override
            public Detail map(ResultSet rs) throws SQLException {
                Detail detail = new Detail();
                detail.setSxid(rs.getInt("sxid"));
                detail.setXprice(rs.getBigDecimal("xprice"));
                detail.setYid(rs.getInt("yid"));
                detail.setNid(rs.getInt("nid"));
                detail.setSid(rs.getInt("sid"));
                return detail;
            }
        },yid,sid);
    }

    class CommodityRowMapperParent implements RowMapper<Commodity>{
        @Override
        public Commodity map(ResultSet rs) throws SQLException {
            Commodity com=new Commodity();
            com.setSid(rs.getInt("sid"));
            com.setSname(rs.getString("sname"));
            com.setUrl(rs.getString("url"));
            com.setSprice(rs.getBigDecimal("sprice"));
            com.setLid(rs.getInt("lid"));
            return com;
        }
    }
     class CommodityRowMapper extends CommodityRowMapperParent{
         @Override
         public Commodity map(ResultSet rs) throws SQLException {
             Commodity com = super.map(rs);
             com.setSxid(rs.getInt("sxid"));
             com.setXprice(rs.getBigDecimal("xprice"));
             com.setNid(rs.getInt("nid"));
             com.setNname(rs.getString("nname"));
             com.setYname(rs.getString("yname"));
             return  com;
         }
     }
    class CommoditymemoryRowMapperParent implements RowMapper<Commoditymemory>{
        @Override
        public Commoditymemory map(ResultSet rs) throws SQLException {
            Commoditymemory commy=new Commoditymemory();
            commy.setNid(rs.getInt("nid"));
            commy.setNname(rs.getString("nname"));
            return commy;
        }
    }
    class CommoditymemoryRowMapper extends CommoditymemoryRowMapperParent{
        @Override
        public Commoditymemory map(ResultSet rs) throws SQLException {
            Commoditymemory com = super.map(rs);
            com.setXprice(rs.getBigDecimal("xprice"));
            com.setSid(rs.getInt("sid"));
            return com;
        }
    }

    class CommoditycolorRowMapper implements RowMapper<Commoditycolor>{
        @Override
        public Commoditycolor map(ResultSet rs) throws SQLException {
            Commoditycolor commc=new Commoditycolor();
            commc.setYid(rs.getInt("yid"));
            commc.setYname(rs.getString("yname"));
            return commc;
        }
    }

    class CommodityimgRowMapper implements RowMapper<Commodityimg>{
        @Override
        public Commodityimg map(ResultSet rs) throws SQLException {
            Commodityimg comimg = new Commodityimg();
            comimg.setTid(rs.getInt("tid"));
            comimg.setTname(rs.getString("tname"));
            comimg.setSid(rs.getInt("sid"));
            comimg.setClassify(rs.getInt("classify"));
            return comimg;
        }
    }

    class DetailRowMapper implements RowMapper<Detail>{
        @Override
        public Detail map(ResultSet rs) throws SQLException {
            Detail detail= new Detail();
            detail.setSxid(rs.getInt("sxid"));
            detail.setXprice(rs.getBigDecimal("xprice"));
            detail.setYid(rs.getInt("yid"));
            detail.setNid(rs.getInt("nid"));
            detail.setSid(rs.getInt("sid"));
            return detail;
        }
    }
}
